import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.print.attribute.AttributeSet;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
/*
 * Created by JFormDesigner on Mon Nov 29 14:13:49 CST 2021
 */



/**
 * @author 123
 */
public class addStudentDialog extends JDialog {
    static int flag =0;
    InitialParam initial = null;
    Connection conn = null;
    public addStudentDialog(Window owner) {
        super(owner);
        initComponents();
        setVisible(true);
        initialConn();

    }

    void initialConn(){
        try {
            initial = new InitialParam();
            conn = initial.conn;
        }catch (Exception e2){
            e2.printStackTrace();
        }
    }

    //当为修改学生信息的情况时,从学生类里面读取数据
    void modifyStudent(){
        if (Student.isNull()){

        }else{
            sno_textfield.setText(Student.sno);
            name_textfield.setText(Student.name);
            if (Student.sex.equals( "男")){
                sexRadioButton1.setSelected(true);
            }else{
                sexRadioButton2.setSelected(true);
            }
            birth_textfield.setText(Student.birth);
            mno_textfield.setText(Student.mno);
            grade_textfield.setText(Student.grade);
        }
    }

    //添加学生信息
    void addStudent(String sno,String name,String sex,String birth,String mno,String grade) throws SQLException {
        String sql = "insert into student(SNO,SNAME,SEX,BIRTH,MNO,GRADE)" +
                " values (?,?,?,?,?,?)";
        if (conn==null){
            JOptionPane.showMessageDialog(addStudentDialog.this,"数据库未连接！","提示",JOptionPane.OK_OPTION);
        }else {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, sno);
            st.setString(2, name);
            st.setString(3, sex);
            st.setString(4, birth);
            st.setString(5, mno);
            st.setString(6, grade);
            if (st.execute()) {
                System.out.println("失败");
                JOptionPane.showConfirmDialog(addStudentDialog.this, "添加失败！", "提示", JOptionPane.OK_OPTION);
            } else {
                System.out.println("成功");
                JOptionPane.showConfirmDialog(addStudentDialog.this, "添加成功！", "提示", JOptionPane.CLOSED_OPTION);
            }
        }

    }

    //更新学生信息
    void updateStudent(String sno,String name,String sex,String birth,String mno,String grade) throws SQLException {
        String sql = "update student set sno=?,sname=?,sex=?,birth=?,mno=?,grade=? where sno = ?";
        if (conn==null){
            JOptionPane.showMessageDialog(addStudentDialog.this,"数据库未连接！","提示",JOptionPane.OK_OPTION);
        }else {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, sno);
            st.setString(2, name);
            st.setString(3, sex);
            st.setString(4, birth);
            st.setString(5, mno);
            st.setString(6, grade);
            st.setString(7, sno);
            if (st.execute()) {
                System.out.println("失败");
                JOptionPane.showConfirmDialog(addStudentDialog.this, "修改失败！", "提示", JOptionPane.OK_OPTION);
            } else {
                System.out.println("成功");
                JOptionPane.showConfirmDialog(addStudentDialog.this, "修改成功！", "提示", JOptionPane.CLOSED_OPTION);
            }
        }
    }

    private void okButtonMouseClicked(MouseEvent e) {
        String sex;
        if (flag == 1){
            if (sexRadioButton1.isSelected()){
                sex= "男";
            }else{
                sex= "女";
            }
            try {
                addStudent(sno_textfield.getText(), name_textfield.getText(), sex, birth_textfield.getText(), mno_textfield.getText(), grade_textfield.getText());
            }catch (Exception e2){

            }
        }else{
            if (sexRadioButton1.isSelected()){
                sex= "男";
            }else{
                sex= "女";
            }
            try {
                updateStudent(sno_textfield.getText(), name_textfield.getText(), sex, birth_textfield.getText(), mno_textfield.getText(), grade_textfield.getText());
            }catch (Exception e2){

            }
        }


    }

    private void birth_textfieldFocusLost(FocusEvent e) {
        if (birth_textfield.getText().isEmpty()){
               birth_textfield.setText("输入格式:2020-1-1");
               birth_textfield.setForeground(Color.GRAY);
        }
    }

    private void birth_textfieldFocusGained(FocusEvent e) {

    }

    private void thisWindowActivated(WindowEvent e) {
        if (birth_textfield.getText().isEmpty()){
            birth_textfield.setText("输入格式:2020-1-1");
            birth_textfield.setForeground(Color.GRAY);
        }

        //判断是添加用户还是修改用户
        if (!Student.isNull()){
            flag = 2; //修改用户
            modifyStudent();
        }else{
            flag =1;//添加用户
        }
    }

    private void cancelButtonMouseClicked(MouseEvent e) {
        Student.putNull();
        dispose();
    }

    private void thisWindowClosing(WindowEvent e) {
        Student.putNull();
    }

    private void birth_textfieldMouseClicked(MouseEvent e) {
        if (birth_textfield.getText().equals("输入格式:2020-1-1")){
            birth_textfield.setText("");
            birth_textfield.setForeground(Color.BLACK);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        this2 = new JPanel();
        sno_textfield = new JTextField();
        label1 = new JLabel();
        label2 = new JLabel();
        name_textfield = new JTextField();
        label3 = new JLabel();
        sexRadioButton1 = new JRadioButton();
        sexRadioButton2 = new JRadioButton();
        birth_textfield = new JTextField();
        label4 = new JLabel();
        label5 = new JLabel();
        mno_textfield = new JTextField();
        label6 = new JLabel();
        grade_textfield = new JTextField();
        label7 = new JLabel();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5b66\u751f\u4fe1\u606f");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                thisWindowActivated(e);
            }
            @Override
            public void windowClosing(WindowEvent e) {
                thisWindowClosing(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //======== this2 ========
                {
                    this2.setLayout(null);

                    //---- sno_textfield ----
                    sno_textfield.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
                    this2.add(sno_textfield);
                    sno_textfield.setBounds(55, 30, 95, sno_textfield.getPreferredSize().height);

                    //---- label1 ----
                    label1.setText("\u5b66\u53f7:");
                    this2.add(label1);
                    label1.setBounds(15, 35, 50, label1.getPreferredSize().height);

                    //---- label2 ----
                    label2.setText("\u59d3\u540d:");
                    this2.add(label2);
                    label2.setBounds(15, 70, 50, 17);
                    this2.add(name_textfield);
                    name_textfield.setBounds(55, 65, 95, 30);

                    //---- label3 ----
                    label3.setText("\u6027\u522b:");
                    this2.add(label3);
                    label3.setBounds(15, 115, 50, 17);

                    //---- sexRadioButton1 ----
                    sexRadioButton1.setText("\u7537");
                    sexRadioButton1.setSelected(true);
                    this2.add(sexRadioButton1);
                    sexRadioButton1.setBounds(60, 115, 55, 21);

                    //---- sexRadioButton2 ----
                    sexRadioButton2.setText("\u5973");
                    this2.add(sexRadioButton2);
                    sexRadioButton2.setBounds(120, 115, 55, 21);

                    //---- birth_textfield ----
                    birth_textfield.addFocusListener(new FocusAdapter() {
                        @Override
                        public void focusGained(FocusEvent e) {
                            birth_textfieldFocusGained(e);
                        }
                        @Override
                        public void focusLost(FocusEvent e) {
                            birth_textfieldFocusLost(e);
                        }
                    });
                    birth_textfield.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            birth_textfieldMouseClicked(e);
                        }
                    });
                    this2.add(birth_textfield);
                    birth_textfield.setBounds(85, 145, 195, 30);

                    //---- label4 ----
                    label4.setText("\u51fa\u751f\u65e5\u671f:");
                    this2.add(label4);
                    label4.setBounds(15, 155, 65, 17);

                    //---- label5 ----
                    label5.setText("MNO:");
                    this2.add(label5);
                    label5.setBounds(185, 35, 50, 17);
                    this2.add(mno_textfield);
                    mno_textfield.setBounds(225, 30, 95, 30);

                    //---- label6 ----
                    label6.setText("\u5e74\u7ea7\uff1a");
                    this2.add(label6);
                    label6.setBounds(185, 75, 50, 17);
                    this2.add(grade_textfield);
                    grade_textfield.setBounds(225, 70, 95, 30);

                    //---- label7 ----
                    label7.setText(" ");
                    this2.add(label7);
                    label7.setBounds(new Rectangle(new Point(300, 200), label7.getPreferredSize()));

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < this2.getComponentCount(); i++) {
                            Rectangle bounds = this2.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = this2.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        this2.setMinimumSize(preferredSize);
                        this2.setPreferredSize(preferredSize);
                    }
                }
                contentPanel.add(this2);
                this2.setBounds(0, 0, 370, 200);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("\u63d0\u4ea4");
                okButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        okButtonMouseClicked(e);
                    }
                });
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("\u5173\u95ed");
                cancelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        cancelButtonMouseClicked(e);
                    }
                });
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(sexRadioButton1);
        buttonGroup.add(sexRadioButton2);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JPanel this2;
    private JTextField sno_textfield;
    private JLabel label1;
    private JLabel label2;
    private JTextField name_textfield;
    private JLabel label3;
    private JRadioButton sexRadioButton1;
    private JRadioButton sexRadioButton2;
    private JTextField birth_textfield;
    private JLabel label4;
    private JLabel label5;
    private JTextField mno_textfield;
    private JLabel label6;
    private JTextField grade_textfield;
    private JLabel label7;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
